export class Car {
    constructor( x, y, color, allow_direction, length ) {
        this.x = x;
        this.y = y;
        this.allow_direction = allow_direction;
        this.length = length;
        this.color = color;
    }

    ResetTo( new_car ) {
        this.x = new_car.x;
        this.y = new_car.y;
    }

    update_type( type ) {
        let [ a, b, c ] = type
        this.color = a
        this.allow_direction = b
        this.length = c
    }
}

export class MainCar extends Car {
    constructor( x, y ) {
        super( x, y, 'red', 'x', 2 );
    }
}

export class OtherCar extends Car {

}

const CAR_COLOR_TABLE = [
    "#b5ce29", "#d1ae04", "#2634af", "#549918", "#1a8eed", "#56ffa5", "#d4db57", "#ed4051", "#a015c6", "#c1cc4f",
    "#e562b7", "#dde84c", "#f93bb1", "#1e4a96", "#1ced34", "#68f2e6", "#c63976", "#b25c1a", "#eb6ef4", "#10b294"
];

/**
 *
 * @param {Array} array
 */
function shuffle( array ) {
    for ( let i = array.length - 1; i > 0; i-- ) {
        const j = Math.floor( Math.random() * ( i + 1 ) );
        [ array[ i ], array[ j ] ] = [ array[ j ], array[ i ] ];
    }
}

shuffle( CAR_COLOR_TABLE );

export function ParseCars( car_list_from_back ) {
    let cars = [];

    car_list_from_back.forEach( ( car_from_back, index ) => {
        if ( index === 0 ) {
            cars.push( new MainCar( car_from_back.position.x, car_from_back.position.y, ) );
            return;
        }

        cars.push( new OtherCar( car_from_back.position.x, car_from_back.position.y, CAR_COLOR_TABLE[ index ],
            car_from_back.allow_direction, car_from_back.length ) );
    } );

    return cars;
}
